package com.tacc.tyun.common.httpclient;

import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.TimeUnit;

/**
 * PoolingHttpClientConnectionManager用来管理客户端的连接池，并且可以为多个线程的请求提供服务
 * @author taocongcong
 * @create 2017-07-11 11:09
 */
@Configuration
@EnableConfigurationProperties(HttpClientProperties.class)//开启属性注入,通过@autowired注入
public class MyPoolingHttpClientConnectionManager {
    @Autowired
    private HttpClientProperties httpClientProperties;

    @Bean
    public PoolingHttpClientConnectionManager poolingClientConnectionManager(){
        PoolingHttpClientConnectionManager poolHttpcConnManager = new PoolingHttpClientConnectionManager(60, TimeUnit.SECONDS);
        // 最大连接数
        poolHttpcConnManager.setMaxTotal(httpClientProperties.getMaxTotal());
        // 路由基数
        poolHttpcConnManager.setDefaultMaxPerRoute(httpClientProperties.getDefaultMaxPerRoute());
        return poolHttpcConnManager;
    }
}
